module.exports = function(lab) {
    return {
        fakeSession: function(session, {id, wechat_openID, wechat_kitty}) {
            return lab.slime.member.@fakeSession(id).then(function(member) {
                session.currentMember = member
                session.wechat_kitty  = wechat_kitty;
                session.wechat_openID = wechat_openID
                this.next(member)
            })
        },
        fakeUserSession: function(session, account) {
            return lab.backend.user.@getByAccount(account).then(function (currentUser) {
                session.currentUser = currentUser;
                this.next(currentUser);
            })
        },
        fakeMemberSession : function (session, phone) {
            return lab.slime.member.@phoneUpdateMember(phone).then(function (currentMember) {
                session.currentMember = currentMember;
                this.next(currentMember);
            })
        },
        fakeSessionByWorkWX: function(session, workwx_userID) {
            session.workwx_kitty = "workwx.connect"
            session.workwx_userID = workwx_userID
            return @.async.resolve()
        },
        importStoreFromJSONRPC : function (session, jsonrpc = "jsonrpc") {
            if (!session.currentOU) {
                throw @lab.errors.not_login;
            }
            return lab[jsonrpc].slime.store.searchStores({
                start: 0,
                limit : -1 
            }).then(function ({list}) {
                @.async.all(list, function (store) {
                    @dump(store);
                    lab.slime_test.doSmartImport(session, "slime_stores", store).pipe(this);
                }).pipe(this);
            })
        },
        importBrandSKUFromJSONRPC: function(session, condition = {}, jsonrpc = "jsonrpc") {
            if (!session.currentOU) {
                throw @lab.errors.not_login;
            }
            var start = 0;
            var limit = 50;
            var skusToImport = [];
            return @.async(function() {
                lab[jsonrpc].slime.brand.searchSKUs(@.merge.simple({}, condition, {
                    start,
                    limit
                })).finished((error, { list }) => {
                    if (error) {
                        @error(error);
                        this.reject(error);
                    } else {
                        if (list.length == 0) {
                            this.next(skusToImport)
                        } else {
                            skusToImport = skusToImport.concat(list);
                            start += limit;
                            this.retry();
                        }
                    }
                })
            }).all(function(sku) {
                return lab.slime.brand.@importSKU(session.currentOU.id, {
                    barcode: sku.barcode,
                    item_code: sku.item_code,
                    retail_unit: sku.retail_unit,
                    name: sku.name,
                    brand: sku.brand,
                    category: sku.category,
                    list_price: sku.list_price,
                    market_price: sku.market_price,
                    image_url: sku.image_url,
                    memo: sku.memo,
                    on_sale: sku.on_sale,
                    tags: sku.tags,
                    spec: sku.spec,
                    weight: sku.weight
                }).then(function (localSKU) {
                    this.next({
                        remote : sku, 
                        local  : localSKU
                    });
                });
            })
        }
    }
}